
package kaizone.haining.chaobohui.fragment;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.json.NewsDO;
import kaizone.haining.chaobohui.json.NewsItem;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.util.Utils;
import kaizone.haining.chaobohui.widget.AdView;
import kaizone.haining.chaobohui.widget.ReHeightImageView;
import kaizone.haining.chaobohui.widget.UsersActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class DetailNewsFragment extends Fragment implements DataControl.IDataUpdateListener {

    public final static String FLAG_NEWS = "flag_news";

    private TextView mTitleTextView;

    private TextView mCopyrightTextView;

    private FrameLayout mImageFrame;

    private TextView mContentTextView;

    private String mDataSourceUrl;

    private DataControl mDataControl;

    private int mRequestId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRequestId = getArguments() != null ? getArguments().getInt(FLAG_NEWS) : 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = (View) inflater.inflate(R.layout.news_detail, null);
        mTitleTextView = (TextView) view.findViewById(R.id.tv01);
        mCopyrightTextView = (TextView) view.findViewById(R.id.tv02);
        mContentTextView = (TextView) view.findViewById(R.id.tv03);
        mImageFrame = (FrameLayout) view.findViewById(R.id.img01);

        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);
        initControl();
        initAd(view);
        attchDataActionBar();
        return view;
    }

    public void initAd(View parent) {
        AdView adView = (AdView) parent.findViewById(R.id.adpanel);
        adView.loadAdBottom();
        adView.rangeWithPanel(getActivity());
    }

    private void initControl() {
        mDataSourceUrl = "http://api.dp-app.com/chao/remote/rest/query_news_detail.htm?id="
                + mRequestId;
        mDataControl.exec(mDataSourceUrl);
    }

    public void attchDataActionBar() {
        if (getActivity() instanceof DetailActivity) {
            UsersActionBar bar = ((DetailActivity) getActivity()).getUsersActionBar();
            if (bar == null)
                return;
            bar.setShareObject(mDataSourceUrl);
        }
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        if (TextUtils.isEmpty(requeststring))
            return null;
        //
        NewsDO newsDO = NewsDO.parseJson(datastring);
        return newsDO;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result != null && result instanceof NewsDO) {
            NewsDO newsDO = (NewsDO) result;
            NewsItem newsItem = newsDO.getData();
            if (newsItem != null) {
                String title = newsItem.getTitle();
                StringBuilder copyright = new StringBuilder();
                String gmtpublish = newsItem.getGmtPublish();
                gmtpublish = Utils.subString(gmtpublish, "T");
                copyright.append(newsItem.getSource()).append("\t\t").append(gmtpublish);
                String content = newsItem.getContent();

                mTitleTextView.setText(title);
                mCopyrightTextView.setText(copyright);
                mContentTextView.setText(content);

                List<PicModel> picmodels = newsItem.getPicModelList();
                if (picmodels != null && !picmodels.isEmpty()) {
                    View imagePanel = getImagePanel(picmodels);
                    if (imagePanel != null) {
                        mImageFrame.addView(imagePanel);
                        mImageFrame.setVisibility(View.VISIBLE);
                    }
                } else {
                    mImageFrame.setVisibility(View.GONE);
                }

                UsersActionBar.attchShareData(getActivity(), title, picmodels);
            }
        } else {

        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    public void setDataSource(String url) {
        mDataSourceUrl = url;
    }

    public int getRequestId() {
        return mRequestId;
    }

    public void setRequestId(int id) {
        this.mRequestId = id;
    }

    public static DetailNewsFragment newInstance(int newsid) {
        final DetailNewsFragment f = new DetailNewsFragment();

        final Bundle args = new Bundle();
        args.putInt(FLAG_NEWS, newsid);
        f.setArguments(args);

        return f;
    }

    public View getImagePanel(List<PicModel> picmodels) {
        if (picmodels == null || picmodels.isEmpty())
            return null;
        if (getActivity() == null)
            return null;
        LinearLayout panel = new LinearLayout(getActivity());
        panel.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        panel.setOrientation(LinearLayout.VERTICAL);

        int count = picmodels.size();
        for (int i = 0; i < count; i++) {
            PicModel picModel = picmodels.get(i);
            ReHeightImageView imageview = new ReHeightImageView(getActivity());
            // imageview.setScaleType(ScaleType.FIT_CENTER);
            final DisplayMetrics displayMetrics = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            final int screenWidth = displayMetrics.widthPixels;
            final int screenHeight = displayMetrics.heightPixels;
            final int imageWidth = screenWidth;
            final int imageHeight = screenWidth * 3 / 4;
            imageview.setLayoutParams(new LinearLayout.LayoutParams(imageWidth,
                    LayoutParams.WRAP_CONTENT));
            // imageview.setAdjustViewBounds(true);
            // imageview.setMaxWidth(imageWidth);
            // imageview.setMaxHeight(imageHeight);
            panel.addView(imageview, i);
            if (getActivity() instanceof DetailActivity) {
                ImageFetcher imageFetcher = ((DetailActivity) getActivity()).getImageFetcher();
                imageFetcher.loadImage(picModel.url, imageview);
            }
        }
        return panel;
    }

}
